package com.ayf.payment.game.api.service.platform.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ayf.payment.game.api.cache.redis.RedisCacheUtil;
import com.ayf.payment.game.api.dto.platform.WxMsgConfigDTO;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.entity.platform.WxMsgConfig;
import com.ayf.payment.game.api.mapper.platform.WxMsgConfigMapper;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.service.platform.WxMsgConfigService;
import com.ayf.payment.game.api.wx.MyConstant;
import com.ayf.payment.game.api.wx.WxMsgType;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class WxMsgConfigServiceImpl implements WxMsgConfigService {

    @Resource
    private WxMsgConfigMapper wxMsgConfigMapper;
    @Autowired
    private WxConfigService wxConfigService;
    @Autowired
    private RedisCacheUtil redisCacheUtil;
    @Autowired
    private MyConstant myConstant;
    @Override
    public String getTempId(String tempIdShort) {
        String key = "GAME_PAY_WX_CONFIG:PLATFORM_WX_TEMP_ID_SHORT_" + tempIdShort;
        String tempId = redisCacheUtil.getObjectFromCache(key);
        if (StringUtils.isEmpty(tempId)) {
            WxMsgConfig wxMsgConfig = wxMsgConfigMapper.selectByPrimaryKey(tempIdShort);
            if (wxMsgConfig == null) {
                WxMsgConfig wxMsgConfig1 = new WxMsgConfig();
                wxMsgConfig1.setTemplateIdShort(tempIdShort);
                wxMsgConfigMapper.insertSelective(wxMsgConfig1);
                wxMsgConfig = wxMsgConfigMapper.selectByPrimaryKey(tempIdShort);
            }
            if (StringUtils.isEmpty(wxMsgConfig.getTemplateId())) {
                WxConfig config = wxConfigService.getConfig();
                String templateId = myConstant.getTemplateId(config, tempIdShort);
                if (StrUtil.isEmpty(templateId)) {
                    return null;
                }
                wxMsgConfig.setTemplateId(templateId);
                wxMsgConfigMapper.updateByPrimaryKey(wxMsgConfig);
                redisCacheUtil.setObjectToCache(key, templateId, 7200L, TimeUnit.SECONDS);
                return templateId;
            } else {
                redisCacheUtil.setObjectToCache(key, wxMsgConfig.getTemplateId(), 7200L, TimeUnit.SECONDS);
                return wxMsgConfig.getTemplateId();
            }
        } else {
            return tempId;
        }
    }

    /**
     * 获取微信公众号模板列表
     * @return
     */
    @Override
    public List<WxMsgConfig> getWxTemplate() {
        return wxMsgConfigMapper.selectAll();
    }

    @Override
    public WxMsgConfigDTO getWxTemplateConfig(String id) {
        WxMsgConfig wxMsgConfig = wxMsgConfigMapper.selectByPrimaryKey(id);
        // 转换json格式
        WxMsgConfigDTO wxMsgConfigDTO = new WxMsgConfigDTO();
        wxMsgConfigDTO.setId(id);
        wxMsgConfigDTO.setTemplateId(id);
        wxMsgConfigDTO.setConfig(wxMsgConfig.getConfig());
        return wxMsgConfigDTO;
    }

    /**
     * 配置模板设置
     *
     * @param wxMsgConfigDTO@return
     */
    @Override
    public boolean setWxTemplateConfig(WxMsgConfigDTO wxMsgConfigDTO) {
        WxMsgConfig wxMsgConfig = new WxMsgConfig();
        // 模板id
        wxMsgConfig.setTemplateId(wxMsgConfigDTO.getTemplateId());
        wxMsgConfig.setConfig(wxMsgConfigDTO.getConfig());
        wxMsgConfig.setTemplateIdShort(wxMsgConfigDTO.getId());
        // 更新数据
        boolean result = wxMsgConfigMapper.updateConfig(wxMsgConfig) > 0;
        //更新redis
        if (StringUtils.isNotBlank(wxMsgConfigDTO.getTemplateId())) {
            String key = "GAME_PAY_WX_CONFIG:PLATFORM_WX_TEMP_ID_SHORT_" + wxMsgConfigDTO.getId();
            Map<String,Object> map = new HashMap<>();
            map.put("templateId",wxMsgConfigDTO.getTemplateId());
            map.put("config",wxMsgConfigDTO.getConfig());
            redisCacheUtil.setObjectToCache(key, map, 7200L, TimeUnit.SECONDS);
        }
        return result;
    }

    /**
     * 获取模板id
     *
     * @param templateIdShort
     * @return
     */
    @Override
    public WxMsgConfig getTempIdNew(String templateIdShort) {

        String key = "GAME_PAY_WX_CONFIG:PLATFORM_WX_TEMP_ID_SHORT_" + templateIdShort;
        Map<String, Object> map = redisCacheUtil.getObjectFromCache(key);
        String tempId = null;
        String msgConfig = null;
        if (map != null){
         tempId = map.get("templateId").toString();
         msgConfig = map.get("config").toString();
        }else{
            map = new HashMap<>();
        }
        WxMsgConfig wxMsgConfig = new WxMsgConfig();
        if (StringUtils.isNotBlank(tempId)) {
            wxMsgConfig.setTemplateId(tempId);
            wxMsgConfig.setTemplateIdShort(templateIdShort);
            wxMsgConfig.setConfig(msgConfig);
        } else {
            wxMsgConfig = wxMsgConfigMapper.selectByPrimaryKey(templateIdShort);
                if (StringUtils.isEmpty(wxMsgConfig.getTemplateId())) {
                    LogPortal.error("【{}】微信公众号模板未进行配置,templateId为空", templateIdShort);
                    return null;
                } else {
                    map.put("templateId", wxMsgConfig.getTemplateId());
                    map.put("config", wxMsgConfig.getConfig());
                    redisCacheUtil.setObjectToCache(key, map, 7200L, TimeUnit.SECONDS);

                }
        }
        return wxMsgConfig;
    }
    @Override
    public WxMsgConfig selectOne(String tempCode) {

        // 根据模板code获取模板信息
        WxMsgConfig config = new WxMsgConfig();
        config.setCode(WxMsgType.BIND_WX.toString());
        return wxMsgConfigMapper.selectOne(config);
    }
}
